package com.xs.lomo;

import org.jivesoftware.smack.XMPPConnection;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CfgActivity extends Activity implements OnClickListener {
	
	private ArrayAdapter<?> adapter;
	private Spinner spComno = null, 
					spBaudrate = null, 
					spDataBit = null, 
					spStopBit = null, 
					spParity = null, 
					spFlowCtl = null; 
	private LomoCtx LomoCtx = null;
	private XMPPConnection g_xsConnCtx = null;
	private String BuddyNameStr = null;
	private TextView tvBuddyName = null;
	private Button  BtCfgCmd = null;
	boolean initFlag = false;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		  
		setContentView(R.layout.cfg); 

		if(false == initFlag){
			LomoCtx = new LomoCtx();
			g_xsConnCtx = LomoCtx.getXmppCtx();
			InitView();
			initFlag = true;
		}
		InitNameTv();
	}
	
	private void InitNameTv(){
		Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        BuddyNameStr = bundle.getString("BuddyName");
		tvBuddyName.setText(BuddyNameStr);
	}
	
	private void InitView(){
		spComno = (Spinner)findViewById(R.id.Spinner_comno);
		spBaudrate = (Spinner)findViewById(R.id.Spinner_baudrate);
		spDataBit =  (Spinner)findViewById(R.id.Spinner_databit);
		spStopBit = (Spinner)findViewById(R.id.Spinner_stopbit);
		spParity = (Spinner)findViewById(R.id.Spinner_parity);
		spFlowCtl = (Spinner)findViewById(R.id.Spinner_ctl);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.com_no, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spComno.setAdapter(adapter);
		spComno.setSelection(0);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.baudrate, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spBaudrate.setAdapter(adapter);
		spBaudrate.setSelection(9);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.databit, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spDataBit.setAdapter(adapter);
		spDataBit.setSelection(0);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.stopbit, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spStopBit.setAdapter(adapter);
		spStopBit.setSelection(1);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.parity, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spParity.setAdapter(adapter);
		spParity.setSelection(0);
		
		adapter = ArrayAdapter.createFromResource(this, R.array.flowctl, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spFlowCtl.setAdapter(adapter);
		spFlowCtl.setSelection(0);
		
		BtCfgCmd = (Button)findViewById(R.id.cfg_cmd_btn);
		BtCfgCmd.setOnClickListener(this);
		
		tvBuddyName = (TextView)findViewById(R.id.tvCfgBuddyName);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int viewId = v.getId();
		String JID = BuddyNameStr+"@"+g_xsConnCtx.getServiceName();
		switch(viewId){
			case R.id.cfg_cmd_btn:
				int iNo = spComno.getSelectedItemPosition();
//				String No = spComno.getSelectedItem().toString();
				String strBaudrate = spBaudrate.getSelectedItem().toString();
				String strDataBit = spDataBit.getSelectedItem().toString();
				int iStopBit = spStopBit.getSelectedItemPosition();
				int iParity = spParity.getSelectedItemPosition();
				int iFlowCtl = spFlowCtl.getSelectedItemPosition();

				LomoCtx.SendCfg(JID, iNo, strBaudrate, strDataBit, iStopBit, iParity, iFlowCtl);
				Toast.makeText(getBaseContext(), iNo+strBaudrate+strDataBit+iStopBit+iParity+iFlowCtl, Toast.LENGTH_SHORT).show();
				break;
		}
	}
}